package gazzera.node.core.plugins.shareplugin;

import gazzera.node.core.plugins.shareplugin.db.*;
import gazzera.utils.*;

import java.io.*;
import java.util.*;

public class ShareList extends HashMap<Integer, Share>
{

	public ShareList()
	{
		super();
	}

	public ShareList(Map<? extends Integer, ? extends Share> arg0)
	{
		super(arg0);
	}
	
	public void test(HashDatabase db)
	{
		scanall(db);
		/*
		System.out.println(this.values());
		
		System.out.println(localToSharedPath("D:\\nicola\\eclipse34\\eclipse\\artifacts.xml"));
		System.out.println(localToSharedPath("D:\\nicola\\eclipse34\\eclipse\\dropins"));
		System.out.println(localToSharedPath("D:\\nicola\\eclipse34\\eclipse"));
		System.out.println(localToSharedPath("D:\\nicola\\dllexportviewer\\readme.txt"));
		System.out.println(localToSharedPath("D:\\nicola\\eclipse34"));
		
		System.out.println(sharedToLocalPath("eclipse34/eclipse/artifacts.xml"));
		System.out.println(sharedToLocalPath("eclipse34/eclipse/dropins"));
		System.out.println(sharedToLocalPath("eclipse34/eclipse"));
		System.out.println(sharedToLocalPath("dllexportviewer/readme.txt"));
		System.out.println(sharedToLocalPath("eclipse34"));
		*/
	}
	
	public Share findShared(File f)
	{
		for (Share share : values())
		{
			if (FileUtil.isParentOf(share.getFolder(), f) || share.getFolder().equals(f.getAbsoluteFile()))
			{
				return share;
			}
		}
		return null;
	}

	public Share findShared(String name)
	{
		for (Share share : values())
		{
			if (share.getFolder().getName().equals(name))
			{
				return share;
			}
		}
		return null;
	}

	
	public boolean isShared(File f)
	{
		return findShared(f)!=null;
	}

	public String localToSharedPath(String localFilename)
	{
		File local = new File(localFilename);
		Share s = findShared(local);
		if (local == null) return null;
		if(local.equals(s.getFolder()))
		{
			return s.getFolder().getName();
		}
		String relative = FileUtil.getRelativePath(s.getFolder(), local, "/");
		String res = s.getFolder().getName()+"/"+relative;
		return res;
	}
	
	public String sharedToLocalPath(String sharedFilename)
	{
		List<String> parts = new ArrayList<String>( Arrays.asList(sharedFilename.split("/")) );
		String sharename = parts.get(0);
		parts.remove(0);
		String relativePath = Str.join(parts, File.separator); 
		
		Share s = findShared(sharename);
		String localPath =  s.getFolder().getAbsolutePath()+ (Str.isEmpty(relativePath) ? "": File.separator+relativePath);
		return localPath;
	}	
	
	private void scanFolder(File folder, HashDatabase db)
	{
		File[] childs = folder.listFiles();
		if(childs==null) return;
		for (File file : childs) {
			if(file.isDirectory())
			{
				scanFolder(file, db);
			}
			else
			{
				// force enqueue if hash empty
				db.getHash(file, true);
			}
		}
	}
	private void scanall(HashDatabase db)
	{
		for (Share s : values()) {
			scanFolder(s.getFolder(), db);
		}
	}
}
